// mircoApp/cultivateCourse/courseClass/courseClassIndex/courseClassIndex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    course_term_list: '',
    page: 1,
    isLoadMore: true,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    this.getData()
  },
  toDetail(e) {
    let { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/mircoApp/cultivateCourse/courseClass/courseClassDetail/courseClassDetail?id=' + id,
    })
  },
  init() {
    this.setData({
      keyword: '',
      course_term_list: '',
      page: 1,
      isLoadMore: true,
      total_page: 0
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.term.course-term"),
      data: {
        keyword: this.data.keyword,
        page: this.data.page
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            course_term_list: res.data.course_term_list.data,
            total_page: res.data.course_term_list.last_page
          })
        } else {
          this.data.isLoadMore = false;
          app.tips(res.smg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    console.log(this.data.page,this.data.total_page,'isLoadMore')
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      wx.showLoading({
        title: '正在加载',
      })
      this.setData({
        page: this.data.page + 1
      })
      urlStr = app.getNetAddresss('plugin.cultivate-course.frontend.controllers.term.course-term');
      app._postNetWork({
        url: urlStr,
        data: {
          keyword: this.data.keyword,
          page: this.data.page
        },
        success: (resdata) => {
          wx.hideLoading();
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              course_term_list: this.data.course_term_list.concat(res.data.course_term_list.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res);
        }
      });
    }
  }
})